package tq.com.tqstudent.bean;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lib.common.utils.SPUtils;

import tq.com.tqstudent.App;
import tq.com.tqstudent.Constants;

/**
 * @Author rames
 * @date 2019/3/8 16:42
 */
public class UserInfo {
    private static final String HEAD_IMAGE = "HEAD_IMAGE";
    private static final String USER_NAME = "USER_NAME";
    private static final String SERVER_HTTP_URL = "SERVER_HTTP_URL"; // 短链的地址
    private static final String SERVER_IM_URL = "SERVER_IM_URL"; // IM的地址
    private static final String SERVER_FILE_URL = "SERVER_FILE_URL"; // 文件服务器的地址


    public static boolean isLogin() {
        return SPUtils.getInstance(App.getApp()).getBoolean(SPUtils.IS_LOGINED);
    }

    public static void loginOut() {
        SPUtils.getInstance(App.getApp()).putBoolean(SPUtils.IS_LOGINED, false);
        SPUtils.getInstance(App.getApp()).putString(SPUtils.USER_INFO, "");
        SPUtils.getInstance(App.getApp()).putString(SPUtils.TOKEN, "");
    }

    public static String getToken() {
        return SPUtils.getInstance(App.getApp()).getString(SPUtils.TOKEN);
    }

    public static void setToken(String token) {
        SPUtils.getInstance(App.getApp()).putString(SPUtils.TOKEN, token);
    }

    public static int getUserID() {
        return SPUtils.getInstance(App.getApp()).getInt(SPUtils.USER_ID);
    }

    public static String getHeadImg() {
        return SPUtils.getInstance(App.getApp()).getString(HEAD_IMAGE);
    }

    public static void setHeadImg(String imageID) {
        if(!TextUtils.isEmpty(imageID)) {
            SPUtils.getInstance(App.getApp()).putString(HEAD_IMAGE, imageID);
        }
    }

    public static String getUserName(){
        return SPUtils.getInstance(App.getApp()).getString(USER_NAME);
    }

    public static void setUserName(String name){
        if(!TextUtils.isEmpty(name)) {
            SPUtils.getInstance(App.getApp()).putString(HEAD_IMAGE, name);
        }
    }

    public static void set(UserBean user) {
        set(user, "");
    }

    public static void set(UserBean user, String token) {
        if (user == null) return;

        Gson gson = new Gson();
        String json = gson.toJson(user);
        SPUtils.getInstance(App.getApp()).putString(SPUtils.USER_INFO, json);
        SPUtils.getInstance(App.getApp()).putBoolean(SPUtils.IS_LOGINED, true);
        SPUtils.getInstance(App.getApp()).putInt(SPUtils.USER_ID, user.id);
        SPUtils.getInstance(App.getApp()).putString(USER_NAME, user.name);
        setHeadImg(user.pic);

        if (!TextUtils.isEmpty(token)) {
            SPUtils.getInstance(App.getApp()).putString(SPUtils.TOKEN, token);
        }
    }

    public static UserBean get() {
        String json = SPUtils.getInstance(App.getApp()).getString(SPUtils.USER_INFO);
        UserBean user;
        Gson gson = new Gson();
        user = gson.fromJson(json, UserBean.class);
        return user;
    }

    /** 接口服务器路径 */
    public static String getHttpUrl(){
        return SPUtils.getInstance(App.getApp()).getString(SERVER_HTTP_URL, Constants.HTTP_URL);
    }

    public static void setHttpUrl(String value){
        SPUtils.getInstance(App.getApp()).putString(SERVER_HTTP_URL, value);
    }

    /** 文件服务器路径 */
    public static String getFileHttpUrl(){
        return SPUtils.getInstance(App.getApp()).getString(SERVER_FILE_URL, Constants.HTTP_FILE_URL);
    }

    public static void setFileHttpUrl(String value){
        SPUtils.getInstance(App.getApp()).putString(SERVER_FILE_URL, value);
    }

    /** 长链接服务器路径 */
    public static String getIMHttpUrl(){
        return SPUtils.getInstance(App.getApp()).getString(SERVER_IM_URL, Constants.HTTP_IM_IP_URL+":"+Constants.HTTP_IM_PORT_URL);
    }

    public static void setIMHttpUrl(String value){
        SPUtils.getInstance(App.getApp()).putString(SERVER_IM_URL, value);
    }
}
